﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class HtmlPageHelper
    {

        public static string SubString(this HtmlHelper helper,string txt)
        {
            
            if (txt.Length<20)
            {
                return txt;
            }
            else
            {
                return txt.Substring(0, 20) + ".....";
            }
             
        }

        // 传一个字符串过来，我给你封装成一个标签：<span>txt</span> 
        public static string GetLable(this HtmlHelper helper, string txt)
        {
            return string.Format("<span>{0}</span>", txt);
        }


        // 传一个字符串过来，我给你封装成一个标签：<span>txt</span> 
        public static MvcHtmlString GetMvcHtmlStringLable(this HtmlHelper helper, string txt)
        {
            string str = string.Format("<span>{0}</span>", txt);
            //将上的str 转成   MvcHtmlString

            // 创建一个实例：new
            //本类有个静态方法帮我们创建 Create
            //y有个工厂帮我们创建一个实例：HttpApplicationFactory
            //return new MvcHtmlString(str);

            //
            return MvcHtmlString.Create(str);
        }

        // 传一个字符串过来，我给你封装成一个标签：<span>txt</span> 
        public static HtmlString GetHtmlStringLable(this HtmlHelper helper, string txt)
        {
            string str = string.Format("<span class='laoma'>{0}</span>", txt);
            //将上的str 转成   MvcHtmlString

            // 创建一个实例：new
            //本类有个静态方法帮我们创建 Create
            //y有个工厂帮我们创建一个实例：HttpApplicationFactory
            //return new MvcHtmlString(str);

            //
            //return MvcHtmlString.Create(str);

            return new HtmlString(str);
        }

        public static HtmlString ShowPageNavigate
            (this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount, string projectId = "")
        {
            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                //if (currentPage != 1)
                {//处理首页连接
                    //output.AppendFormat("<li><a  href='{0}?pageIndex=1&pageSize={1}'>首页</a></li> ", redirectTo, pageSize);
                    if (projectId == "")
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex=1&pageSize={1}'>First</a></span>",
                        redirectTo, pageSize);
                    }
                    else
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex=1&pageSize={1}&projectId={2}'>First</a></span>",
                        redirectTo, pageSize, projectId);
                    }
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    //output.AppendFormat("<li><a  href='{0}?pageIndex={1}&pageSize={2}'>上一页</a></li> ", redirectTo, currentPage - 1, pageSize);
                    if (projectId == "")
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}'>Prev</a></span>", redirectTo, currentPage - 1, pageSize);
                    }
                    else
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}&projectId={3}'>Prev</a></span>", redirectTo, currentPage - 1, pageSize,projectId);
                    }
                }
                else
                {
                    // output.Append("<span class='pageLink'>上一页</span>");
                }

                //output.Append(" ");
                output.Append("<span>");
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码，前面5个，后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理
                            //output.Append(string.Format("[{0}]", currentPage));
                            //output.AppendFormat("<li class='active'><a  href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li> ", redirectTo, currentPage, pageSize, currentPage);
                            if (projectId=="")
                            {
                                output.AppendFormat("<span class='btn btn-info'><a href='{0}?pageIndex={1}&pageSize={2}'> {3}</a></span> ", redirectTo, currentPage, pageSize, currentPage);
                            }
                            else
                            {
                                output.AppendFormat("<span class='btn btn-info'><a href='{0}?pageIndex={1}&pageSize={2}&projectId={4}'> {3}</a></span> ", redirectTo, currentPage, pageSize, currentPage,projectId);
                            }
                        }
                        else
                        {//一般页处理
                            //output.AppendFormat("<li><a  href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
                            if (projectId == "")
                            {
                                output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></span> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
                            }
                            else
                            {
                                output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}&projectId={4}'>{3}</a></span> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint,projectId);
                            }
                        }
                    }
                    //output.Append(" ");
                    output.Append("</span>");
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    //output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>下一页</a></li> ", redirectTo, currentPage + 1, pageSize);
                    output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}'>Next</a></span>", redirectTo, currentPage + 1, pageSize);
                }
                else
                {
                    //output.Append("<span class='pageLink'>下一页</span>");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    //output.AppendFormat("<li><a  href='{0}?pageIndex={1}&pageSize={2}'>末页</a></li> ", redirectTo, totalPages, pageSize);
                    if (projectId=="")
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}'>Last</a></span>", redirectTo, totalPages, pageSize);
                    }
                    else
                    {
                        output.AppendFormat("<span class='btn btn-default'><a href='{0}?pageIndex={1}&pageSize={2}&projectId={3}'>Last</a></span>", redirectTo, totalPages, pageSize,projectId);
                    }
                }
                output.Append(" ");
            }
            //output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行

            return new HtmlString(output.ToString());
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id">分页id</param>
        /// <param name="currentPageIndex">当前页</param>
        /// <param name="pageSize">分页尺寸</param>
        /// <param name="recordCount">记录总数</param>
        /// <param name="className">分页样式</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
        {
            return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id">分页id</param>
        /// <param name="currentPageIndex">当前页</param>
        /// <param name="pageSize">分页尺寸</param>
        /// <param name="recordCount">记录总数</param>
        /// <param name="htmlAttributes">分页头标签属性</param>
        /// <param name="className">分页样式</param>
        /// <param name="mode">分页模式</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className)
        {
            TagBuilder builder = new TagBuilder("div");
            builder.IdAttributeDotReplacement = "_";
            builder.GenerateId(id);
            builder.AddCssClass(className);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount);
            return builder.ToString();
        }


        /// <summary>
        /// 获取普通分页
        /// </summary>
        /// <param name="currentPageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount)
        {
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            StringBuilder url = new StringBuilder();
            url.Append(HttpContext.Current.Request.Url.AbsolutePath + "?page={0}");
            NameValueCollection collection = HttpContext.Current.Request.QueryString;
            string[] keys = collection.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i].ToLower() != "page")
                    url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<span style=\"float:left\">总共{0}条记录,共{1}页,当前第{2}页&nbsp;&nbsp;&nbsp;&nbsp;</span> ", recordCount, pageCount, currentPageIndex);
            //sb.Append("<ul>");

            //if (currentPageIndex == 1)
            //    sb.Append("<span>首页</span> ");
            //else
            //{
            //    string url1 = string.Format(url.ToString(), 1);
            //    sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1);
            //}
            if (currentPageIndex > 1)
            {
                string url1 = string.Format(url.ToString(), currentPageIndex - 1);
                //sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1);
                sb.AppendFormat("<a class='btn btn-default' href=\"{0}\" >&lt;</a>", url1);
            }
            //else
            //    sb.Append("<li  class=\"prev\">上一页</li> ");

            sb.Append(GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString()));

            if (currentPageIndex < pageCount)
            {
                string url1 = string.Format(url.ToString(), currentPageIndex + 1);
                //sb.AppendFormat("<span><a href={0}>下一页</a></span> ", url1);
                sb.AppendFormat("<a class='btn btn-default' href=\"{0}\" >&gt;</a>", url1);
            }
            //else
            //    sb.Append("<li  class=\"next\">下一页</li> ");

            //if (currentPageIndex == pageCount)
            //   sb.Append("<span>末页</span> ");
            //else
            //{
            //    string url1 = string.Format(url.ToString(), pageCount);
            //    sb.AppendFormat("<span><a href={0}>末页</a></span> ", url1);
            //}

            //sb.Append("</ul>");

            return sb.ToString();
        }
        /// <summary>
        /// 获取数字分页
        /// </summary>
        /// <param name="currentPageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="pageCount"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url)
        {
            int k = currentPageIndex / 10;
            int m = currentPageIndex % 10;
            StringBuilder sb = new StringBuilder();
            if (currentPageIndex / 10 == pageCount / 10)
            {
                if (m == 0)
                {
                    k--;
                    m = 10;
                }
                else
                    m = pageCount % 10;
            }
            else
                m = 10;
            for (int i = k * 10 + 1; i <= k * 10 + m; i++)
            {
                if (i == currentPageIndex)
                    sb.AppendFormat("<span><font color=red><b>{0}</b></font></span> ", i);
                else
                {
                    string url1 = string.Format(url.ToString(), i);
                    sb.AppendFormat("<span><a href='{0}'>{1}</a></span> ", url1, i);
                }
            }

            return sb.ToString();
        }



    }
}